package com.basic.library.binding.viewadapter.sectionrecycleview;


import android.view.View;

import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.basic.library.binding.viewadapter.recyclerview.EasyAdapterModel;
import com.basic.library.binding.viewadapter.recyclerview.LineManagers;
import com.basic.library.binding.viewadapter.recyclerview.ListFactory;
import com.basic.library.brvah.MBaseViewHolder;
import com.basic.library.brvah.MSimpleClickListener;
import com.basic.library.brvah.MyBaseMultiItemQuickAdapter;
import com.basic.library.brvah.MyBaseQuickAdapter;
import com.basic.library.view.hover.SuspensionRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import me.tatarka.bindingcollectionadapter2.LayoutManagers;

public class ViewAdapter {

    @BindingAdapter(value = {"bindAdapter", "layoutManager", "bindItems", "bindSimpleItemClick", "bindItemChildClick", "setLoadMore"}, requireAll = false)
    public static <T, K extends MBaseViewHolder> void initAdapter(SuspensionRecycleView suspensionRecycleView
            , BaseQuickAdapter<T, K> adapter
            , LayoutManagers.LayoutManagerFactory layoutManagerFactory
            , ArrayList<T> list
            , MSimpleClickListener mSimpleClickListener
            , BaseQuickAdapter.OnItemChildClickListener listener
            , BaseQuickAdapter.RequestLoadMoreListener loadMoreListener) {
        View v = suspensionRecycleView.getChildAt(0);
        RecyclerView recyclerView = (RecyclerView) v;
        if (adapter != null && recyclerView.getAdapter() == null)
            try {
                recyclerView.setLayoutManager(layoutManagerFactory.create(recyclerView));
                if (loadMoreListener != null)
                    adapter.setOnLoadMoreListener(loadMoreListener);
                adapter.bindToRecyclerView(recyclerView);
                //没数据的时候不初始化数据，第一次获取真实数据的时候setNewData，可以避免提前setNewData
//                if (list == null) {
//                    list = new ArrayList<>(adapter.getData());
//                }
                if (list instanceof ObservableArrayList) {
                    ((ObservableArrayList) list).addOnListChangedCallback(ListFactory.getListChangedCallback(adapter));
                }
                if (list != null)
                    if (adapter instanceof MyBaseQuickAdapter) {
                        MyBaseQuickAdapter adapter1 = (MyBaseQuickAdapter) adapter;
                        adapter1.initData(list);
                    } else if (adapter instanceof MyBaseMultiItemQuickAdapter) {
                        MyBaseMultiItemQuickAdapter adapter1 = (MyBaseMultiItemQuickAdapter) adapter;
                        adapter1.initData(list);
                    }
                if (mSimpleClickListener != null)
                    recyclerView.addOnItemTouchListener(mSimpleClickListener);
                if (listener != null)
                    adapter.setOnItemChildClickListener(listener);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    /**
     * bindEasyAdapter自动绑定了SwipeRefreshLayout的刷新监听（见方法 setOnRefreshListener）
     * <p>
     * SwipeRefreshLayout的下拉刷新动画在自定义BaseQuickAdapter中写了（见方法 chargeRefresh）；
     * 不过对于搜索框等触发的调用接口无法自动触发下拉动画（主要是下拉开始动画在自定义BaseQuickAdapter找不到合适的触发时机），
     * 这种情况在BaseActivityKt有另外操作（见方法 loading dismiss）
     * <p>
     * mSimpleClickListener和onItemChildClickListener一般只需要一种，多了容易搞错
     *
     * @param suspensionRecycleView
     * @param adapterModel
     * @param <T>
     * @param <K>
     */
    @BindingAdapter(value = {"bindEasyAdapter"}, requireAll = true)
    public static <T, K extends MBaseViewHolder> void initEasyAdapter(SuspensionRecycleView suspensionRecycleView
            , EasyAdapterModel<T, K> adapterModel) {
        View v = suspensionRecycleView.getChildAt(0);
        RecyclerView recyclerView = (RecyclerView) v;
        BaseQuickAdapter<T, K> adapter = adapterModel.adapter;
        LayoutManagers.LayoutManagerFactory layoutManagerFactory = adapterModel.layoutManagerFactory;
        ArrayList<T> list = adapterModel.initList;
        MSimpleClickListener mSimpleClickListener = adapterModel.mSimpleClickListener;
        BaseQuickAdapter.OnItemChildClickListener listener = adapterModel.onItemChildClickListener;
        BaseQuickAdapter.RequestLoadMoreListener loadMoreListener = adapterModel.loadMoreListener;
        LineManagers.LineManagerFactory lineManagerFactory = adapterModel.lineManagerFactory;
        if (adapter != null && recyclerView.getAdapter() == null)
            try {
                //自动绑定监听
                if (adapterModel.onRefreshListener != null) {
                    setOnRefreshListener(recyclerView, adapterModel.onRefreshListener);
                }
                recyclerView.setLayoutManager(layoutManagerFactory.create(recyclerView));
                if (loadMoreListener != null)
                    adapter.setOnLoadMoreListener(loadMoreListener);
                adapter.bindToRecyclerView(recyclerView);
                if (lineManagerFactory != null)
                    recyclerView.addItemDecoration(lineManagerFactory.create(recyclerView));
                //没数据的时候不初始化数据，第一次获取真实数据的时候setNewData，可以避免提前setNewData
//                if (list == null) {
//                    list = new ArrayList<>(adapter.getData());
//                }
                if (list instanceof ObservableArrayList) {
                    ((ObservableArrayList) list).addOnListChangedCallback(ListFactory.getListChangedCallback(adapter));
                }
                if (list != null)
                    if (adapter instanceof MyBaseQuickAdapter) {
                        MyBaseQuickAdapter adapter1 = (MyBaseQuickAdapter) adapter;
                        adapter1.initData(list);
                    } else if (adapter instanceof MyBaseMultiItemQuickAdapter) {
                        MyBaseMultiItemQuickAdapter adapter1 = (MyBaseMultiItemQuickAdapter) adapter;
                        adapter1.initData(list);
                    }
                if (mSimpleClickListener != null)
                    recyclerView.addOnItemTouchListener(mSimpleClickListener);
                if (listener != null)
                    adapter.setOnItemChildClickListener(listener);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    /**
     * SwipeRefreshLayout最多2层，够用了
     */
    public static void setOnRefreshListener(RecyclerView recyclerView, SwipeRefreshLayout.OnRefreshListener onRefreshListener) {
        if (recyclerView != null) {
            if (recyclerView.getParent() instanceof SwipeRefreshLayout)
                ((SwipeRefreshLayout) recyclerView.getParent()).setOnRefreshListener(onRefreshListener);
            else if (recyclerView.getParent().getParent() instanceof SwipeRefreshLayout)
                ((SwipeRefreshLayout) recyclerView.getParent().getParent()).setOnRefreshListener(onRefreshListener);
        }
    }
}
